<?php

class Userv2mobile_model extends Base_Model
{
    const TABLE = 'user_v2_mobile_code';

    public function __construct()
    {
        $this->load->database();
    }

    /**
     * 添加验证码
     *
     * @param $data
     * @return bool|int
     */
    public function addSmsCode($data)
    {
        if($this->db->insert(self::TABLE, $data)){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    /**
     * 获取验证码
     *
     * @param $where
     * @return array
     */
    public function getSmscode($where)
    {
        $query = $this->db->select('code,created')->order_by('created desc')
                      ->get_where(self::TABLE, $where)->row_array();
        if(!empty($query)){
        	$sub = time() - $query['created'];
        	if($sub > 1200){
        		return array('status'=>false,'msg'=>'短信验证码已过期','code'=>'','created'=>$query['created']);
        	}else{return array('status'=>true,'msg'=>'短信验证通过','code'=>$query['code'],'created'=>$query['created']);}
        }else {return array('status'=>false,'msg'=>'短信验证码没有发送','code'=>'','created'=>'');}
    }
}